<?php
$this->template->add_admin_css_lib('layout.css');
$this->template->add_admin_css_lib('jquery.gritter.css');
$this->template->add_admin_css_lib('menu_manage.css');
$this->template->add_admin_js_lib('jquery.gritter.min.js');

$this->template->add_admin_js_lib("jquery.nestable.js");
function print_menu2($menu) {
	echo '<li class="dd-item" data-id="'.$menu->term_id.'" data-type="'.$menu->term_type.'" data-value="'.@$menu->men_value.'">'
			.'<div class="dd-handle">'.$menu->term_name.' </div><i class="dd-remove fa fa-trash-o"></i><i class="dd-edit fa fa-pencil"></i>';
	if (!empty($menu->child)) {
		echo '<ol class="dd-list">';
		foreach ($menu->child as $item) print_menu2($item);
		echo '</ol>';
	}
	echo '</li>';
}
?>

<div class="row">
<div class="col-sm-6">
  <!-- START panel-->
  <div class="panel panel-default">
	 <div class="panel-heading">Thêm mới</div>
	 <div class="panel-body">
		<form role="form">
		   <div class="form-group">
			  <label>Tên:</label>
			  <input type="text" placeholder="Tên" class="form-control" id="_linkname" value="">
		   </div>
		   <div class="form-group">
			  <label>Slug:</label>
			  <input type="text" placeholder="Slug" class="form-control" id="_slug">
		   </div> 
		   <div class="form-group">
			  <label>Trạng thái:</label>
			  <select name="edit[can_delete]" id="status" class="form-control "><option value="1" selected="selected">Hiện</option><option value="0">Ẩn</option></select>
		   </div>
		   
		   <button type="submit" class="btn btn-sm btn-default" id="add_url">Thêm</button>
		</form>
	 </div>
  </div>
  <!-- END panel-->
</div>
<div class="col-sm-6">
  <!-- START panel-->
  <div class="panel panel-default">
  <div class="panel-heading">Quản lý - Sắp xếp vị trí</div>
	<div class="panel-bodyx">
		<form action="" method="post" class="form-horizontal">

<div class="cf nestable-lists">
	<div class="dd" id="nestable">
		<ol class="dd-list">
		<?php
		
			if (isset($menu_tree) && $menu_tree) foreach ($menu_tree as $menu) print_menu2($menu);
		?>
		</ol>
		
		
		<button type="button" class="btn btn-primary btn-small" id="saveorder">
		OK
	</button> 
	or 
	<a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
	</div>
	<textarea id="nestable-output" style="display: none;"></textarea>
</div>

</div>



</form>
	
  </div>
  <!-- END panel-->
</div>
</div>



<script>
	var group = $("ul.serialization").sortable({
		group: 'serialization',
		//delay: 500,
		onDrop: function (item, container, _super) {
			 var data = group.sortable("serialize").get();
			 var jsonString = JSON.stringify(data, null, ' ');
			// console.log(data);
			// $('#serialize_output2').text(jsonString);
			_super(item, container)
		}
	});
</script>


<script type="text/javascript">
$(function (){
	$('#add_url').click(function (){
		//system_update_notif();
		var link = $('#_link').val();
		var status = $('#status').val();
		var name = $('#_linkname').val();
		var slug = $('#_slug').val();
		if(name =='')
		{
			alert('Tên không được để trống');
			return false;
		}
		var url_add_menu  = '<?php echo $this->url_index; ?>/add_url/<?php echo @$menu_id;?>';
		if ($('li[data-id='+$('#url_task').val()+']').size() > 0) {
			//ton tai
			$('li[data-id='+$('#url_task').val()+']').find('.dd-handle').text(name);
			$('li[data-id='+$('#url_task').val()+']').attr('data-value', link);
		}
		
		$.post (url_add_menu, {
			'men_name': name,
			'men_type': '<?php echo @$menu_type; ?>',
			'men_parent': <?php echo @$menu_id; ?>,
			'status': status,
			'slug': slug,
			'men_id': $('#url_task').val(),
		}, function (re){
			try {
				var id = parseInt(re);
				if ($('li[data-id='+id+']').size() > 0) {
					//ton tai
					$(".btn-notify-error").attr('data-message','Thêm không thành công, đã tồn tại id này');
					$(".btn-notify-error").click();
				}else {
					$('#nestable > ol').append('<li class="dd-item" data-id="'+id+'" data-type="url"><div class="dd-handle">'+name+' </div><i class="dd-remove fa fa-trash-o"></i><i class="dd-edit fa fa-pencil"></i></li>');
					$(".btn-notify-success").attr('data-message','Thêm thành công');
					$(".btn-notify-success").click();
				}
			 $('#_link').val('');
			 $('#_linkname').val('');
			 $('#url_task').val('');
			}catch (e) {
				alert("Không thể thêm danh mục");
			}
			//$.gritter.removeAll();
		})
		return false;
	});
	$('#cate').change(function (){
		$('#catetitle').val($(this).find('option:selected').text().replace(/^([\s-]+)/, ''));
	});
	$('#add_cate').click(function (){
		system_update_notif();
		var link = $('#cate').val();
		var name = $('#catetitle').val();
		var url_add_menu  = '<?php echo admin_url(); ?>menu/add_url/<?php echo @$menu_id;?>';
		if ($('li[data-id='+$('#cate_task').val()+']').size() > 0) {
			//ton tai
			$('li[data-id='+$('#cate_task').val()+']').find('.dd-handle').text(name);
			$('li[data-id='+$('#cate_task').val()+']').attr('data-value', link);
		}
		$.post (url_add_menu, {
			'men_name': name,
			'men_type': 'cat',
			'men_parent': <?php echo @$menu_id; ?>,
			'men_link': link,
			'type': <?php echo @$menu_type; ?>,
			'men_id': $('#cate_task').val(),
		}, function (re){
			console.log(re);
			$('#cate_task').val('');
			try {
				var id = parseInt(re);
				$('#nestable > ol').append('<li class="dd-item" data-id="'+id+'" data-type="url"><div class="dd-handle">'+name+' </div><i class="dd-remove glyphicon glyphicon-remove"></i></li>');
			 $('#_link').val('');
			 $('#_linkname').val('');
			}catch (e) {
				alert("Không thể thêm danh mục");
			}
			$.gritter.removeAll();
		})
	});
});
</script>
<script>
	var group = $("ul.serialization").sortable({
		group: 'serialization',
		//delay: 500,
		onDrop: function (item, container, _super) {
			 var data = group.sortable("serialize").get();
			
			 var jsonString = JSON.stringify(data, null, ' ');
			console.log(data);
		//	$('#serialize_output2').text(jsonString);
			_super(item, container)
		}
	});
</script>
<button type="button" data-toggle="notify" data-message="Message with status.." data-options="{&quot;status&quot;:&quot;danger&quot;}" class="btn btn-danger btn-notify-error" style="display: none;">Danger</button>
<button type="button" data-toggle="notify" data-message="Cập nhật thành công." data-options="{&quot;status&quot;:&quot;success&quot;}" class="btn btn-success btn-notify-success" style="display: none;">Success</button>

<script type="text/javascript">

$(document).ready(function()
{
    var updateOutput=function(e)
	{var list=e.length?e:$(e.target),output=list.data('output');if(!output){return false;}
	if(window.JSON){output.val(window.JSON.stringify(list.nestable('serialize')));}else{output.val('JSON browser support required for this demo.');}};if($('#nestable').size()>0)$('#nestable').nestable({group:1}).on('change',updateOutput);$('#nestable2').nestable({group:1}).on('change',updateOutput);updateOutput($('#nestable').data('output',$('#nestable-output')));$('#nestable-menu').on('click',function(e)
	{var target=$(e.target),action=target.data('action');if(action==='expand-all'){$('.dd').nestable('expandAll');}
	if(action==='collapse-all'){$('.dd').nestable('collapseAll');}});$('#nestable3').nestable();
	
	$('#saveorder').click(function(){
		$.post('<?php echo $this->url_index; ?>/save_order/<?php echo @$menu_id;?>', {
			json: $('#nestable-output').val()
		}, function (re){
			$(".btn-notify-success").click();
		});
		return false;
	});
	$(document.body).on('click', 'i.dd-remove', function (){
		var id = $(this).parent().attr('data-id');
		$(this).parent().remove();
		$.post('<?php echo $this->url_index; ?>/del_child/'+id, {}, function (re){
			$(".btn-notify-success").attr('data-message','Xóa thành công.');
			$(".btn-notify-success").click();
		});
		return false;
	});
	$(document.body).on('click', 'i.dd-edit', function (){
		var p = $(this).parent();
		var value = $(p).attr('data-value');
		var a_target=  '';
		var id = $(p).attr('data-id');
		window.location = "<?php echo $this->url_edit;?>"+id;
		return false;
	});
	
});
</script>



<style type="text/css">
</style>